package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.entity.TFork;
import com.cookor.poi.modules.phy.service.TForkService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/fork"})
@ResponseBody
public class ApiForkController extends ApiBaseController {
  @Autowired
  private TForkService tForkService;
  
  @RequestMapping({"/add"})
  public ResultUtil addFork(HttpServletRequest request, String openId, Integer doctorId, Integer status) {
    ResultUtil result = getResult();
    try {
      if (status == null || StringUtils.isBlank(openId) || doctorId == null)
        throw new Exception("参数错误"); 
      this.tForkService.addFork(openId, doctorId, status);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiForkController addFork error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/list"})
  public ResultUtil forkList(HttpServletRequest request, String openId) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      TFork fork = new TFork();
      fork.setTenantId(tenantId);
      fork.setOpenId(openId);
      fork.setStatus(Integer.valueOf(1));
      Page<TFork> page = this.tForkService.findPage(new Page(1, 10), fork);
      result.putParams("page", page);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiForkController forkList error!", e);
    } 
    return result;
  }
}
